const tabsView = {
    namespaced: true,
    state: {
        visitedViews: []
    },
    mutations: {
        ADD_VISITED_VIEWS: (state, view) => {
            if (state.visitedViews.some(v => v.path === view.path)) {
                return
            }
            state.visitedViews.push({
                name: view.name,
                path: view.path,
                title: view.title || 'no-name'
            })
        },
        DEL_VISITED_VIEWS: (state, name) => {
            for (const [i, v] of state.visitedViews.entries()) {
                if (v.name === name) {
                    state.visitedViews.splice(i, 1)
                    break
                }
            }
        },
        DEL_OTHERS_VIEWS: (state, name, whiteListName) => {
            const whiteObjArr = whiteListName && whiteListName.length ? state.visitedViews.filter(item => whiteListName.includes(item.name)) : []
            for (const [i, v] of state.visitedViews.entries()) {
                if (v.name === name) {
                    state.visitedViews = whiteListName.includes(name) ? state.visitedViews.slice(i, i + 1) : [...whiteObjArr, ...state.visitedViews.slice(i, i + 1)]
                    break
                }
            }
        },
        DEL_ALL_VIEWS: (state, whiteListName) => {
            const arr = state.visitedViews.filter(item => {
                return whiteListName.includes(item.name)
            })
            state.visitedViews = arr
        }
    },
    actions: {
        addVisitedViews({ commit }, view) {
            commit('ADD_VISITED_VIEWS', view)
        },
        delVisitedViews({ commit, state }, name) {
            return new Promise(resolve => {
                commit('DEL_VISITED_VIEWS', name)
                resolve([...state.visitedViews])
            })
        },
        delOthersViews({ commit, state }, name) {
            return new Promise(resolve => {
                commit('DEL_OTHERS_VIEWS', name)
                resolve([...state.visitedViews])
            })
        },
        delAllViews({ commit, state }, whiteListName) {
            return new Promise(resolve => {
                commit('DEL_ALL_VIEWS', whiteListName)
                resolve([...state.visitedViews])
            })
        }
    }
}
export default tabsView